<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class MessageController extends AppUserBaseC {

    //站内消息列表
    public function actionMsgList() {
        $model = MessageModel::model();
        //分页
        $pageId = (int) Yii::app()->request->getParam('offset');
        //分页数
        $limit = (int) Yii::app()->request->getParam('num');
        if (empty($limit)) {
            $limit = 20;
        }
        $where = array(
            'order' => 'datetime desc,nid desc',
            'userid' => $this->uid,
        );
        $criteria = $model->where($where);
        $count = $model->count($criteria);
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $dataList = $model->findAll($criteria);
        if (empty($dataList)) {
            $this->ajaxReturn(array('status' => 1, 'info' => '信息为空', 'detail' => '', 'count' => 0));
        }
        //打开次页面表示已经阅读
        $data = $nids = array();
        foreach ($dataList as $k => $xx) {
            $data[$k]['nid'] = $xx->nid;
            $data[$k]['userid'] = $xx->userid;
            $data[$k]['typeid'] = $xx->typeid;
            $data[$k]['title'] = $xx->title;
            $data[$k]['is_read'] = $xx->is_read;
            $data[$k]['extend_params'] = unserialize($xx->extend_params);
            $data[$k]['sendname'] = $xx->sendname;
            $data[$k]['senduid'] = $xx->senduid;
            $data[$k]['datetime'] = $xx->datetime;
            if ($xx['is_read']) {
                continue;
            }
            $nids[$xx['nid']] = $xx['nid'];
        }
        if (!empty($nids)) {
            $model->noticeRead($nids, $this->uid);
        }
        $this->ajaxReturn(array('status' => 1, 'info' => '读取消息成功', 'count' => $count, 'detail' => $data));
    }

    //是否有新消息

    public function actionIsNew() {
        $model = new MessageModel();
        $count = (int) $model->getNoticeNoReadCount($this->uid);
        if (empty($count)) {
            $this->ajaxReturn(array('status' => 0, 'info' => '您没有未读消息!', 'count' => 0));
        } else {
            $this->ajaxReturn(array('status' => 1, 'info' => '您有' . $count . '条未读消息!', 'count' => $count));
        }
    }

    //标记为已读

    public function actionRead() {
        $model = new MessageModel();
        $nid = Yii::app()->request->getParam('nid');
        if ($model->noticeRead($nid)) {
            $this->ajaxReturn(array('status' => true, 'info' => '已标记为已读'));
        } else {
            $this->ajaxReturn(array('status' => false, 'info' => '标记为已读失败'));
        }
    }

    //删除消息

    public function actionDelete() {
        $model = new MessageModel();
        $nid = Yii::app()->request->getParam('nid');
        if ($model->noticeDel($this->uid, $nid)) {
            $this->ajaxReturn(array('status' => true, 'info' => '删除成功!'));
        } else {
            $this->ajaxReturn(array('status' => false, 'info' => '删除失败!'));
        }
    }

}
